#include "print.h"
#include "init.h"
#include "thread.h"
#include "interrupt.h"
#include "console.h"
#include "process.h"
#include "syscall-init.h"
#include "syscall.h"
#include "stdio.h"
#include "memory.h"
#include "dir.h"
#include "fs.h"
#include "assert.h"
#include "shell.h"

#include "ide.h"
#include "stdio-kernel.h"

void init(void);

int main(void) {
   put_str("I am kernel\n");
   init_all();

/*************    写入应用程序    *************/
   uint32_t file_size = 14348; 
   uint32_t sec_cnt = DIV_ROUND_UP(file_size, 512);
   struct disk* sda = &channels[0].devices[1];
   void* prog_buf = sys_malloc(sec_cnt * 512);
   ide_read(sda, 300, prog_buf, sec_cnt);
   int32_t fd = sys_open("/prog_no_arg", O_CREAT|O_RDWR);
   if (fd != -1) {
      if(sys_write(fd, prog_buf, file_size) == -1) {
         printk("file write error!\n");
         while(1);
      }
   }
   sys_free(prog_buf);
/*************    写入应用程序结束   *************/
/*************    写入应用程序2    *************/
   uint32_t file_size2 = 14344; 
   uint32_t sec_cnt2 = DIV_ROUND_UP(file_size2, 512);
   struct disk* sda2 = &channels[0].devices[1];
   void* prog_buf2 = sys_malloc(sec_cnt2 * 512);
   ide_read(sda2, 350, prog_buf2, sec_cnt2);
   int32_t fd2 = sys_open("/prog_arg", O_CREAT|O_RDWR);
   if (fd2 != -1) {
      if(sys_write(fd2, prog_buf2, file_size2) == -1) {
         printk("file write error!\n");
         while(1);
      }
   }
   sys_free(prog_buf2);
/*************    写入应用程序2结束   *************/
//   cls_screen();
   console_put_str("[rabbit@localhost /]$ ");

   while(1);
   return 0;
}

/* init进程 */
void init(void) {
   uint32_t ret_pid = fork();
   if(ret_pid) {  // 父进程
      while(1);
   } else {	  // 子进程
      my_shell();
   }
   panic("init: should not be here");
}
